package com.lgkj.ui.shop;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lgkj.R;
import com.lgkj.bean.meitao.ComfirOrderBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.ui.userinfo.OrderActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.utils.TitleBuilder;
import com.tsy.sdk.pay.alipay.Alipay;
import com.tsy.sdk.pay.weixin.WXPay;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by Administrator on 2018/10/9 0009.
 */

public class PayOrderActivity extends BaseActivity {
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    private List<ComfirOrderBean> cargoosbean;
    private String payType = "1";
    private String order_no = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_order;
    }

    @Override
    public void initView() {
        tvPrice.setText(getIntent().getStringExtra("tvTotalPrice"));
        order_no = getIntent().getStringExtra("order_no");
        new TitleBuilder(PayOrderActivity.this).setRIghtText("我的订单", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startsActivity(new Intent(PayOrderActivity.this, OrderActivity.class));
            }
        }).setTitleText("支付").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private String orderInfo = "";

    @Override
    public void initData() {
        cargoosbean = (List<ComfirOrderBean>) SharedPreferencesUtils.getBean(PayOrderActivity.this, "carinfo");
        if (cargoosbean == null && cargoosbean.size() == 0) {
            return;
        }
        Log.i(TAG, "initData: " + cargoosbean.size());
        Log.i(TAG, "initData: " + cargoosbean.get(0).OrderInfo());
        for (int i = 0; i < cargoosbean.size(); i++) {
            Log.i(TAG, "initData: " + cargoosbean.get(i).OrderInfo());
            orderInfo = orderInfo + cargoosbean.get(i).OrderInfo();
        }
        Log.i(TAG, "initData: " + orderInfo);
    }

    public void save(View view) {
        showDiaLog("正在支付");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("pay_type", payType);  // 1微信2支付宝
        map.put("order_no", order_no);
        HttpUtils.postHttp(map, CommonConstant.pay, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {

                    if (payType.equals("2")) {

                        zfbPay(jsonObject.optJSONObject("data").optString("result"));
                    } else if (payType.equals("1")) {
                        wxPay(jsonObject.optJSONObject("data"));
                    }
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }


    private void wxPay(JSONObject object) {
        if (object == null) {
            return;
        }
        JSONObject result = object.optJSONObject("result");
        if (result == null) {
            return;
        }
        doWXPay(result.toString());
    }


    /**
     * 微信支付
     *
     * @param pay_param 支付服务生成的支付参数
     */
    private void doWXPay(String pay_param) {
        WXPay.init(getApplicationContext(), CommonConstant.WX_APP_ID);      //要在支付前调用
        WXPay.getInstance().doPay(pay_param, new WXPay.WXPayResultCallBack() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplication(), "支付成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(int error_code) {
                switch (error_code) {
                    case WXPay.NO_OR_LOW_WX:
                        Toast.makeText(getApplication(), "未安装微信或微信版本过低", Toast.LENGTH_SHORT).show();
                        break;

                    case WXPay.ERROR_PAY_PARAM:
                        Toast.makeText(getApplication(), "参数错误", Toast.LENGTH_SHORT).show();
                        break;

                    case WXPay.ERROR_PAY:
                        Toast.makeText(getApplication(), "支付失败", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplication(), "支付取消", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void zfbPay(final String orderInfo) {
        new Alipay(this, orderInfo, new Alipay.AlipayResultCallBack() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplication(), "支付成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDealing() {
                Toast.makeText(getApplication(), "支付处理中...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(int error_code) {
                switch (error_code) {
                    case Alipay.ERROR_RESULT:
                        Toast.makeText(getApplication(), "支付失败:支付结果解析错误", Toast.LENGTH_SHORT).show();
                        break;

                    case Alipay.ERROR_NETWORK:
                        Toast.makeText(getApplication(), "支付失败:网络连接错误", Toast.LENGTH_SHORT).show();
                        break;

                    case Alipay.ERROR_PAY:
                        Toast.makeText(getApplication(), "支付错误:支付码支付失败", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(getApplication(), "支付错误", Toast.LENGTH_SHORT).show();
                        break;
                }

            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplication(), "支付取消", Toast.LENGTH_SHORT).show();
            }
        }).doPay();
    }

    @OnClick({R.id.rb_zfb, R.id.rb_wx})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_zfb:
                btnSave.setText("支付宝支付");
                payType = "2";
                break;
            case R.id.rb_wx:
                payType = "1";
                btnSave.setText("微信支付");
                break;
        }
    }

}
